package com.qf.service.impl;

import com.qf.entity.Stock;
import com.qf.mapper.StockMapper;
import com.qf.vo.Goods;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qf.mapper.GoodsMapper;
import com.qf.entity.GoodsEntity;
import com.qf.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {

    @Autowired
    private StockMapper stockMapper;

    @Override
    @Transactional
    public Boolean saveGoodsAndGstock(Goods goods) {

        // 1.先插入商品信息
        // 把goods中的信息拷贝到GoodsEntity中
        GoodsEntity goodsEntity = new GoodsEntity();
        BeanUtils.copyProperties(goods, goodsEntity); // 只要属性名称一样就会拷贝过去
        super.save(goodsEntity);
        goods.setId(goodsEntity.getId());

        // 2.在插入库存信息
        Stock stock = new Stock();
        stock.setGid(goodsEntity.getId());
        stock.setStock(goods.getGstock());
        int insert1 = stockMapper.insert(stock);

        return true;
    }
}